import { Component, OnInit, Inject } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { NzMessageService } from 'ng-zorro-antd'
import { STColumn, STData } from '@delon/abc'
import { ACLService } from '@delon/acl'

import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { Store, IStoreQueryParams } from '@shared/interfaces/store'
import { I18NService } from '@core'
import { StoreService } from 'app/routes/store/store.service'
import { StoreAlinterService } from 'app/routes/store/store-alinter.service'
import { PageService } from '@shared/services/page.service'
import { ETabScrollStatus } from '@shared/enums'
import { openDownloadDialog } from 'app/utils/helpers'
import { ModalService } from '@shared/services/modal.service'

@Component({
  selector: 'app-merchant-detail-tab-store',
  templateUrl: './tab-store.component.html',
  styleUrls: ['../../../../shared/styles/table.less'],
  providers: [
    { provide: StoreService, useClass: StoreAlinterService },
  ]
})
export class MerchantDetailTabStoreComponent extends ListBaseClass<Store, IStoreQueryParams> implements OnInit {
  size = 5
  params: any

  columns: STColumn[] = []

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    public route: ActivatedRoute,
    public router: Router,
    private storeService: StoreService,
    private pageService: PageService,
    private aclService: ACLService,
    private modalService: ModalService,
  ) {
    super(msg, i18n, route, router)
  }

  ngOnInit() {
    this.initColumns()
    const intMerCode  = this.route.parent.snapshot.params['id']
    this.params = { intMerCode, size: this.size }
    this.queryTableDataWithParams()
  }

  queryTableDataWithParams(): void {
    this.loading = true
    this.storeService
      .getList(this.params)
      .subscribe(this.renderList.bind(this))
  }

  renderList(res): void {
    this.data = res.data
    this.total = res.total
    this.loading = false
    this.pageService.setTabScrollStatus(ETabScrollStatus.Scroll)
  }

  viewDetail(data) {
    const { common } = data
    this.router.navigate(['biz/stores', common.intStoreCode])
  }

  /** 导出单条 */
  export(record: STData): void {
    const { intStoreCode } = record.common
    const params: IStoreQueryParams = {
      page: this.params.page,
      size: this.params.size,
      intStoreCode,
    }
    this.goExport(params)
  }

  /** 批量导出 */
  exportAll(): void {
    this.goExport(this.params)
  }

  /** 冻结对话框 */
  freezeDialog(record: Store): void {
    const { storeName } = record.common
    const msg = this.trans('GENERAL.NOTIFICATION.STORE_FREEZE_CONFIRM', { storeName })
    this.modalService.freezeAndUnFreeze(msg).subscribe(isOK => {
      if (!isOK) return
      this.freeze(record)
    })
  }

  /** 解冻对话框 */
  unfreezeDialog(record: Store): void {
    const { storeName } = record.common
    const msg = this.trans('GENERAL.NOTIFICATION.STORE_UNFREEZE_CONFIRM', { storeName })
    this.modalService.freezeAndUnFreeze(msg).subscribe(isOK => {
      if (!isOK) return
      this.unfreeze(record)
    })
  }

  /** 冻结 */
  freeze(record: Store): void {
    const { intStoreCode } = record.common
    this.storeService.freeze(intStoreCode).subscribe(
      success => {
        this.msg.success(this.trans('GENERAL.NOTIFICATION.FREEZE_SUCCESS'))
        this.st.reload()
      },
      err => {
        this.msg.error(this.trans('GENERAL.NOTIFICATION.FREEZE_FAIL'))
      },
    )
  }

  /** 解冻 */
  unfreeze(record: Store): void {
    const { intStoreCode } = record.common
    this.storeService.unfreeze(intStoreCode).subscribe(
      success => {
        this.msg.success(this.trans('GENERAL.NOTIFICATION.UNFREEZE_SUCCESS'))
        this.st.reload()
      },
      err => {
        this.msg.error(this.trans('GENERAL.NOTIFICATION.UNFREEZE_FAIL'))
      },
    )
  }

  private goExport(params: IStoreQueryParams): void {
    this.storeService.getStoreReport(params).subscribe(data => {
      const { url } = data
      openDownloadDialog(url, '导出门店')
    })
  }
  initColumns() {
    const canLock = this.aclService.canAbility('api.put.camel.store.one.freeze')
    const canUnlock = this.aclService.canAbility('api.put.camel.store.one.unfreeze')
    const canDownload = this.aclService.canAbility('api.get.camel.stores.report')
    this.columns = [
      {
        type: 'link',
        title: '门店编号',
        i18n: 'GENERAL.STORE.NUM',
        index: 'common.storeNum',
        width: 160,
        className: 'text-center',
        fixed: 'left',
        click: this.viewDetail.bind(this)
      },
      {
        title: '门店名称',
        i18n: 'GENERAL.STORE.NAME',
        index: 'common.storeName',
        width: 200,
        className: 'text-left',
      },
      {
        title: '门店英文名称',
        i18n: 'GENERAL.STORE.NAME_EN',
        index: 'common.storeNameEN',
        width: 200,
        className: 'text-left',
      },
      {
        title: '创建时间',
        i18n: 'GENERAL.INFO.CREATED_AT',
        index: 'createdAt',
        type: 'date',
        width: 200,
        dateFormat: 'YYYY-MM-DD HH:mm',
        className: 'text-center',
      },
      {
        title: 'Operation',
        i18n: 'GENERAL.INFO.OPERATION',
        width: 180,
        fixed: 'right',
        className: 'text-center',
        iif: () => canLock || canDownload,
        buttons: [
          {
            text: '',
            icon: {
              iconfont: 'icon-download-twotone',
              type: 'iconfont',
            },
            click: this.export.bind(this),
            iif: () => canDownload,
          },
          {
            text: '',
            icon: {
              iconfont: 'icon-lock-twotone',
              type: 'iconfont',
            },
            click: this.freezeDialog.bind(this),
            iif: record => canLock && record.common.status === 'normal',
          },
          {
            text: '',
            icon: {
              iconfont: 'icon-unlock-circle',
              type: 'iconfont',
            },
            click: this.unfreezeDialog.bind(this),
            iif: record => canUnlock && record.common.status === 'freeze',
          }
        ]
      }
    ]
  }
}
